//
//  MyStoreHeader.swift
//  Wanke
//
//  Created by BO ZHOU on 2020/10/9.
//

import UIKit

open class ZBNavigationView: UIView {

    /** 其他项目按钮点击事件 */
    var rightButtonClickBlock: (()->Void)?
    
    /** 左侧返回按钮点击事件 */
    var leftButtonBlock: (()->Void)?
    
    var title: String = "" {
        didSet {
            self.titleLabel.text = title
            
            self.layoutIfNeeded()
        }
    }
    
    lazy var subContentView: UIView = {
       
        let view = UIView()
        
        return view
    }()
    
    /** 左侧按钮 (返回) */
    lazy var leftButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "fanhui"), for: .normal)
//        btn.setImage(UIImage.named_lib("fanhui"), for: .normal)
        btn.image_size = CGSize(width: 12, height: 12)
        btn.imageView?.contentMode = .scaleAspectFill
        
        btn.clickBlock = { [unowned self] (btn) in
            self.leftButtonBlock?()
        }
        
        return btn
    }()
    

    /** 其他项目 */
    lazy var rightButton: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("其他项目", for: .normal)
        btn.layer.cornerRadius = 4
        btn.titleLabel?.font = UIFont.mediumFont(size: 10)
        btn.backgroundColor = UIColor.white.withAlphaComponent(0.06)
        
        btn.clickBlock = { [unowned self] (btn) in
            
            self.rightButtonClickBlock?()
        }
        
        return btn
    }()
    
    /** titleLabel */
    lazy var titleLabel: UILabel = {
       
        let label = UILabel()
        label.text = "闭店申请"
        label.font = UIFont.mediumFont(size: 16)
        label.textColor = .white
        label.sizeToFit()
        label.textAlignment = .center
        
        return label
    }()
    
    let rightButtonHeight: CGFloat = 23
    var rightButtonWidth: CGFloat = 48

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    required public init?(coder: NSCoder) {
        super.init(coder: coder)
        
        setup()
    }
    
    func setup() {
        
        addSubview(subContentView)
        
        subContentView.addSubview(leftButton)
        
        // 自定义右侧按钮
        subContentView.addSubview(rightButton)
        
        subContentView.addSubview(titleLabel)
        
        self.backgroundColor = UIColor.clear
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        subContentView.frame = CGRect(x: 0, y: STATUS_BAR_HEIGHT, width: self.width, height: 44)
        
        leftButton.frame = CGRect(x: 14, y: 0, width: 30, height: 30)
        leftButton.centerY = subContentView.height/2
        
        rightButton.frame = CGRect(x: subContentView.width - 12 - rightButtonWidth, y: 0, width: rightButtonWidth, height: rightButtonHeight)
        
        rightButton.centerY = subContentView.height/2
        
        titleLabel.center = subContentView.relativeCenter
    }

}
